<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/3/15
 * Time: 11:05
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class AlbumAlbumsModel
 * @package SDXApi\Model
 */
class AlbumTemplateAlbumsModel extends Model
{
	/**
	 * @var string
	 */
	protected $tableName = 'album_template_albums';

	/**
	 * Created by Jerry
	 * @param int $templateId
	 * @return mixed
	 */
	public function findAlbumByAlbumId($templateId = 0)
	{
		$time = time();
		$where = array(
			'aa.template_id'	=>	$templateId,
			'a.name'			=>	array('NEQ', ''),
			'a.start_time'		=>	array('ELT', $time),
			'a.end_time'		=>	array('EGT', $time)
		);

		$field = 'a.id, a.`name`, a.small, a.description';

		return $this->alias('aa')
			->field($field)
			->join('LEFT JOIN __ALBUM__ AS a ON aa.value = a.id')
			->where($where)
			->group('aa.value')
			->select();
	}

	/**
	 * Created by Jerry
	 * @param int $id
	 * @return mixed
	 */
	public function findAlbumFourByAlbumId($id = 0)
	{
		$where = array(
			't.album_id'	=>	$id,
			't.type'		=>	2,
		);

		$field = 'tg.goods_id, g.original_img';

		return $this->table('sdx_album_template')
							->field($field)
							->alias('t')
							->join('LEFT JOIN __ALBUM_TEMPLATE_GOODS__ AS tg ON t.id = tg.template_id')
							->join('LEFT JOIN __GOODS__ AS g ON tg.goods_id = g.goods_id')
							->where($where)
							->limit(4)
							->group('tg.goods_id')
							->select();
	}
}